package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.EmPropertiesHistoryBo;
import com.ruoyi.system.domain.vo.EmPropertiesHistoryVo;
import com.ruoyi.system.domain.EmPropertiesHistory;
import com.ruoyi.system.mapper.EmPropertiesHistoryMapper;
import com.ruoyi.system.service.IEmPropertiesHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 不动产-历史Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-04
 */
@RequiredArgsConstructor
@Service
public class EmPropertiesHistoryServiceImpl extends ServiceImpl<EmPropertiesHistoryMapper,EmPropertiesHistory> implements IEmPropertiesHistoryService {


    /**
     * 查询不动产-历史
     */
    @Override
    public EmPropertiesHistoryVo queryById(Long emId){
        return baseMapper.selectVoById(emId);
    }

    /**
     * 查询不动产-历史列表
     */
    @Override
    public TableDataInfo<EmPropertiesHistoryVo> queryPageList(EmPropertiesHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmPropertiesHistory> lqw = buildQueryWrapper(bo);
        Page<EmPropertiesHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询不动产-历史列表
     */
    @Override
    public List<EmPropertiesHistoryVo> queryList(EmPropertiesHistoryBo bo) {
        LambdaQueryWrapper<EmPropertiesHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmPropertiesHistory> buildQueryWrapper(EmPropertiesHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmPropertiesHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEmCode()), EmPropertiesHistory::getEmCode, bo.getEmCode());
        lqw.like(StringUtils.isNotBlank(bo.getEmName()), EmPropertiesHistory::getEmName, bo.getEmName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmType()), EmPropertiesHistory::getEmType, bo.getEmType());
        lqw.eq(StringUtils.isNotBlank(bo.getEmStatus()), EmPropertiesHistory::getEmStatus, bo.getEmStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getEmCompany()), EmPropertiesHistory::getEmCompany, bo.getEmCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getEmLocation()), EmPropertiesHistory::getEmLocation, bo.getEmLocation());
        lqw.eq(bo.getEmArea() != null, EmPropertiesHistory::getEmArea, bo.getEmArea());
        lqw.eq(StringUtils.isNotBlank(bo.getEmOwnershipS()), EmPropertiesHistory::getEmOwnershipS, bo.getEmOwnershipS());
        lqw.like(StringUtils.isNotBlank(bo.getEmOwnershipName()), EmPropertiesHistory::getEmOwnershipName, bo.getEmOwnershipName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmOwnershipMix()), EmPropertiesHistory::getEmOwnershipMix, bo.getEmOwnershipMix());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMortgageY()), EmPropertiesHistory::getEmMortgageY, bo.getEmMortgageY());
        lqw.like(StringUtils.isNotBlank(bo.getEmMortgageName()), EmPropertiesHistory::getEmMortgageName, bo.getEmMortgageName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmCertificateN()), EmPropertiesHistory::getEmCertificateN, bo.getEmCertificateN());
        lqw.eq(StringUtils.isNotBlank(bo.getEmCertificateF()), EmPropertiesHistory::getEmCertificateF, bo.getEmCertificateF());
        lqw.eq(StringUtils.isNotBlank(bo.getEmCertificateT()), EmPropertiesHistory::getEmCertificateT, bo.getEmCertificateT());
        lqw.eq(StringUtils.isNotBlank(bo.getEmLandnature()), EmPropertiesHistory::getEmLandnature, bo.getEmLandnature());
        lqw.eq(bo.getEmHoldingtime() != null, EmPropertiesHistory::getEmHoldingtime, bo.getEmHoldingtime());
        lqw.eq(StringUtils.isNotBlank(bo.getEmHoldingway()), EmPropertiesHistory::getEmHoldingway, bo.getEmHoldingway());
        lqw.eq(bo.getEmTao() != null, EmPropertiesHistory::getEmTao, bo.getEmTao());
        lqw.eq(StringUtils.isNotBlank(bo.getEmFinAccount()), EmPropertiesHistory::getEmFinAccount, bo.getEmFinAccount());
        lqw.eq(bo.getEmFinBook() != null, EmPropertiesHistory::getEmFinBook, bo.getEmFinBook());
        lqw.eq(bo.getEmFinObook() != null, EmPropertiesHistory::getEmFinObook, bo.getEmFinObook());
        lqw.eq(StringUtils.isNotBlank(bo.getEmEval()), EmPropertiesHistory::getEmEval, bo.getEmEval());
        lqw.eq(bo.getEmEvalPrice() != null, EmPropertiesHistory::getEmEvalPrice, bo.getEmEvalPrice());
        lqw.eq(bo.getEmEvalTime() != null, EmPropertiesHistory::getEmEvalTime, bo.getEmEvalTime());
        lqw.eq(StringUtils.isNotBlank(bo.getEmEvalAgency()), EmPropertiesHistory::getEmEvalAgency, bo.getEmEvalAgency());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInPaper()), EmPropertiesHistory::getEmInPaper, bo.getEmInPaper());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInBc1()), EmPropertiesHistory::getEmInBc1, bo.getEmInBc1());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInBc2()), EmPropertiesHistory::getEmInBc2, bo.getEmInBc2());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInBc3()), EmPropertiesHistory::getEmInBc3, bo.getEmInBc3());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInLabel()), EmPropertiesHistory::getEmInLabel, bo.getEmInLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInFlocation()), EmPropertiesHistory::getEmInFlocation, bo.getEmInFlocation());
        lqw.eq(bo.getEmMonthIdle() != null, EmPropertiesHistory::getEmMonthIdle, bo.getEmMonthIdle());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMonthIdleR()), EmPropertiesHistory::getEmMonthIdleR, bo.getEmMonthIdleR());
        lqw.eq(bo.getEmMonthUse() != null, EmPropertiesHistory::getEmMonthUse, bo.getEmMonthUse());
        lqw.eq(StringUtils.isNotBlank(bo.getEmOwnWay()), EmPropertiesHistory::getEmOwnWay, bo.getEmOwnWay());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsWay()), EmPropertiesHistory::getEmMerchantsWay, bo.getEmMerchantsWay());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsPlm()), EmPropertiesHistory::getEmMerchantsPlm, bo.getEmMerchantsPlm());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsUnitw()), EmPropertiesHistory::getEmMerchantsUnitw, bo.getEmMerchantsUnitw());
        lqw.eq(bo.getEmMerchantsDj() != null, EmPropertiesHistory::getEmMerchantsDj, bo.getEmMerchantsDj());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelPaper()), EmPropertiesHistory::getEmMeexcelPaper, bo.getEmMeexcelPaper());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelBc1()), EmPropertiesHistory::getEmMeexcelBc1, bo.getEmMeexcelBc1());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelBc2()), EmPropertiesHistory::getEmMeexcelBc2, bo.getEmMeexcelBc2());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelBc3()), EmPropertiesHistory::getEmMeexcelBc3, bo.getEmMeexcelBc3());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelLabel()), EmPropertiesHistory::getEmMeexcelLabel, bo.getEmMeexcelLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelFlocation()), EmPropertiesHistory::getEmMeexcelFlocation, bo.getEmMeexcelFlocation());
        lqw.eq(bo.getEmMerchantsNumb() != null, EmPropertiesHistory::getEmMerchantsNumb, bo.getEmMerchantsNumb());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgreementNo()), EmPropertiesHistory::getEmAgreementNo, bo.getEmAgreementNo());
        lqw.eq(bo.getEmAgreementPrice() != null, EmPropertiesHistory::getEmAgreementPrice, bo.getEmAgreementPrice());
        lqw.eq(bo.getEmMerchantsUnitp() != null, EmPropertiesHistory::getEmMerchantsUnitp, bo.getEmMerchantsUnitp());
        lqw.eq(bo.getEmMerchantsTotal() != null, EmPropertiesHistory::getEmMerchantsTotal, bo.getEmMerchantsTotal());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsObject()), EmPropertiesHistory::getEmMerchantsObject, bo.getEmMerchantsObject());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsReal()), EmPropertiesHistory::getEmMerchantsReal, bo.getEmMerchantsReal());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsYn()), EmPropertiesHistory::getEmMerchantsYn, bo.getEmMerchantsYn());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsHistory()), EmPropertiesHistory::getEmMerchantsHistory, bo.getEmMerchantsHistory());
        lqw.eq(bo.getEmMerchantsYears() != null, EmPropertiesHistory::getEmMerchantsYears, bo.getEmMerchantsYears());
        lqw.eq(bo.getEmMerchantsBegin() != null, EmPropertiesHistory::getEmMerchantsBegin, bo.getEmMerchantsBegin());
        lqw.eq(bo.getEmMerchantsEnd() != null, EmPropertiesHistory::getEmMerchantsEnd, bo.getEmMerchantsEnd());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsCollectionway()), EmPropertiesHistory::getEmMerchantsCollectionway, bo.getEmMerchantsCollectionway());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsPayway()), EmPropertiesHistory::getEmMerchantsPayway, bo.getEmMerchantsPayway());
        lqw.eq(bo.getEmDepossit() != null, EmPropertiesHistory::getEmDepossit, bo.getEmDepossit());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelPaper()), EmPropertiesHistory::getEmAgexcelPaper, bo.getEmAgexcelPaper());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelBc1()), EmPropertiesHistory::getEmAgexcelBc1, bo.getEmAgexcelBc1());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelBc2()), EmPropertiesHistory::getEmAgexcelBc2, bo.getEmAgexcelBc2());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelBc3()), EmPropertiesHistory::getEmAgexcelBc3, bo.getEmAgexcelBc3());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelLabel()), EmPropertiesHistory::getEmAgexcelLabel, bo.getEmAgexcelLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelFlocation()), EmPropertiesHistory::getEmAgexcelFlocation, bo.getEmAgexcelFlocation());
        lqw.eq(bo.getEmOweTotal() != null, EmPropertiesHistory::getEmOweTotal, bo.getEmOweTotal());
        lqw.eq(StringUtils.isNotBlank(bo.getEmLiquWay()), EmPropertiesHistory::getEmLiquWay, bo.getEmLiquWay());
        lqw.eq(bo.getEmRentAble() != null, EmPropertiesHistory::getEmRentAble, bo.getEmRentAble());
        lqw.eq(bo.getEmRentPaid() != null, EmPropertiesHistory::getEmRentPaid, bo.getEmRentPaid());
        lqw.eq(bo.getEmLateStart() != null, EmPropertiesHistory::getEmLateStart, bo.getEmLateStart());
        lqw.eq(bo.getEmLateAllday() != null, EmPropertiesHistory::getEmLateAllday, bo.getEmLateAllday());
        lqw.eq(bo.getEmLateMoney() != null, EmPropertiesHistory::getEmLateMoney, bo.getEmLateMoney());
        lqw.eq(bo.getEmLateForpay() != null, EmPropertiesHistory::getEmLateForpay, bo.getEmLateForpay());
        lqw.eq(bo.getEmOweLong() != null, EmPropertiesHistory::getEmOweLong, bo.getEmOweLong());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelPaper()), EmPropertiesHistory::getEmRenexcelPaper, bo.getEmRenexcelPaper());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelBc1()), EmPropertiesHistory::getEmRenexcelBc1, bo.getEmRenexcelBc1());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelBc2()), EmPropertiesHistory::getEmRenexcelBc2, bo.getEmRenexcelBc2());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelBc3()), EmPropertiesHistory::getEmRenexcelBc3, bo.getEmRenexcelBc3());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelLabel()), EmPropertiesHistory::getEmRenexcelLabel, bo.getEmRenexcelLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelFlocation()), EmPropertiesHistory::getEmRenexcelFlocation, bo.getEmRenexcelFlocation());
        lqw.eq(bo.getFormDataType() != null, EmPropertiesHistory::getFormDataType, bo.getFormDataType());
        lqw.eq(bo.getCreateDept() != null, EmPropertiesHistory::getCreateDept, bo.getCreateDept());
        lqw.eq(bo.getProcessInstanceId() != null, EmPropertiesHistory::getProcessInstanceId, bo.getProcessInstanceId());
        lqw.eq(bo.getRecordCreateTime() != null, EmPropertiesHistory::getRecordCreateTime, bo.getRecordCreateTime());
        lqw.eq(bo.getStartUserId() != null, EmPropertiesHistory::getStartUserId, bo.getStartUserId());
        return lqw;
    }

    /**
     * 新增不动产-历史
     */
    @Override
    public Boolean insertByBo(EmPropertiesHistoryBo bo) {
        EmPropertiesHistory add = BeanUtil.toBean(bo, EmPropertiesHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEmId(add.getEmId());
        }
        return flag;
    }

    /**
     * 修改不动产-历史
     */
    @Override
    public Boolean updateByBo(EmPropertiesHistoryBo bo) {
        EmPropertiesHistory update = BeanUtil.toBean(bo, EmPropertiesHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmPropertiesHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除不动产-历史
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
